import React from 'react';
import './assistantTherapy.less';
import {browserHistory} from 'react-router';
import { Input, Button, DatePicker, Row, Col, Card,Pagination,message} from 'antd';
import config from '../../config/config';
import $ from 'jquery';
const token = sessionStorage.getItem('token');
import getFormatDate from '../../common/getFormatData';
const allRouter=config.router;
const InputGroup = Input.Group;
let userInfo = JSON.parse(sessionStorage.getItem('UserInfo')); //用户信息
class AssistantTherapy extends React.Component {
    constructor(props){
        super(props);
        this.state={
            tabKey:2,//除了切换页面，
            patientInfos:{data:[]},//所有要接诊患者
            date: undefined, // 时间
            data1:'',//时间字符串
            Inkey: '',// 关键字
            pagesize: 12,// 每页显示的页数
            pageindex: 1,// 当前页
            patientOriginList:[],//患者来源列表
        }
    }
    componentWillMount() {
        let userInfo = JSON.parse(sessionStorage.getItem('UserInfo')); //用户信息
        // let date=getFormatDate(new Date());
        // this.setState({
        //     data1:date
        // });
        //获取患者列表
        this.getAssistantTherapyInfo(JSON.parse(sessionStorage.getItem('UserInfo')).id,1,2);
    }
    componentDidMount() {}
    changeTab(key) {
        this.setState({
            tabKey: key,
            Inkey: "",
            data1:'',
            date:undefined
        });
        this.getAssistantTherapyInfo(JSON.parse(sessionStorage.getItem('UserInfo')).id,1,key,'',);

    }
    // 获取外科治疗列表   surgicalTreatment/list  post
    getAssistantTherapyInfo(doctorId,pageindex,status,searchtext,starttime,endtime,){
        let url=allRouter.patientOrigin+'/surgicalTreatment/list';
        $.ajax({
            url: url,
            type: "post",
            async: true,
            traditional: true,
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            data:{
                doctor_id:doctorId,//医生id
                pageindex, //页码
                pagesize:12,//每页个数
                status,//标识今日接诊 历史接诊 2:表示今日接诊  4:表示历史接诊
                idFree:true,  // idFree是否收费 bool  都是已收费
                searchtext,//搜索关键字
                starttime,
                endtime,
            },
            success:data => {
                if(data.code===200){
                    //因为收费没有完成，所以先不做判断
                    let data2=data.data.filter(key=>key.Order!==null).reverse();
                    data.data=data2;
                    this.setState({
                        patientInfos:data,
                    },()=>{
                        console.log('患者信息 list');
                        console.log(this.state.patientInfos);
                    })
                }else{
                    message.info('搜索失败，请重新操作!');
                }

            },
            error:err=>{
                console.log(err);
            }
        });
    }
    // 日期
    changeDate(date, str) {
        console.log(date);
        console.log(str);
        this.setState({
            date,
            data1:str,
        });
        userInfo = JSON.parse(sessionStorage.getItem('UserInfo'));
        this.getAssistantTherapyInfo(userInfo.id,1,this.state.tabKey,this.state.Inkey,str,str);
    }
    //搜索关键字
    changeInkey(e) {
        this.setState({
            Inkey: e.target.value
        })
    }
    //搜索
    searchRoles(e) {
        let value = e.target.value || this.state.Inkey;
        console.log(value);
        this.getAssistantTherapyInfo(JSON.parse(sessionStorage.getItem('UserInfo')).id,1,this.state.tabKey,value,this.state.data1,this.state.data1);
    }
    //分页
    changePage(pageindex) {
        this.setState({
            pageindex
        });
        this.getAssistantTherapyInfo(JSON.parse(sessionStorage.getItem('UserInfo')).id,pageindex,this.state.tabKey,this.state.Inkey,this.state.data1,this.state.data1);
    }
    //待理疗处理函数
    waitForTreat(item){
        if(item.patient.status===2){
            let url=allRouter.patientOrigin+'/registrationinfor/update ';
            $.ajax({
                url:url,
                type:'POST',
                headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
                data:{
                    payerid:item.patient._id,
                    state:3,
                    orderid:item.orderid,
                },
                success:data => {
                    console.log(data);
                    if(data.code===200){
                        browserHistory.push({pathname:'treatPatientsForm',state:JSON.stringify(item)});
                    }else{
                        console.log('修改患者进入接诊中状态失败');
                        // message.info('修改患者进入');
                    }
                },
                error:err=>{
                    console.log(err);
                }
            })
        }else{
            browserHistory.push({pathname:'treatPatientsForm',state:JSON.stringify(item)});
        }

    }
    render(){
        return (
            <div className="container">
                <div className="doctor-section-box">
                    <div className='btnBox'>
                        <Button type={this.state.tabKey === 2 ? 'primary' : ''} onClick={() => this.changeTab(2)}>今日接诊</Button>
                        <Button style={{marginLeft:'10px'}} type={this.state.tabKey === 4 ? 'primary' : ''} onClick={() => this.changeTab(4)}>历史接诊</Button>
                        <div className='role-manage-box-search my-clear-fix employee-manage-box-search'>
                            <div className='addetd-z employee-manage-search'>
                                <InputGroup >
                                    {this.state.tabKey === 4 ? <DatePicker style={{width:'600px'}}
                                                placeholder= '请选择日期'
                                                onChange={this.changeDate.bind(this)}
                                                format='YYYY-MM-DD'
                                                value={this.state.date}
                                    />:''}
                                    <Input
                                        placeholder="请输入患者姓名或手机号"
                                        value={this.state.Inkey}
                                        onChange={(e) => this.changeInkey(e)}
                                        onPressEnter={(e)=>this.searchRoles(e)}
                                    />
                                </InputGroup>
                            </div>
                            <div className="role-manage-right">
                                <Button type="primary" className='member-button' icon="search" onClick={this.searchRoles.bind(this)} >搜索</Button>
                            </div>
                        </div>
                    </div>
                </div>
                {
                    <div className='addpatient'>
                        <div style={{ background: '#ECECEC', padding: '30px' }}>
                            <Row gutter={16}>
                                {this.state.patientInfos.data?this.state.patientInfos.data.length?this.state.patientInfos.data.map(item=>{
                                    return <Col span={8} key={item._id} style={{marginBottom:'20px'}}>
                                        <Card bordered={false}>
                                            <img src={require('../../../images/patientDefault.png')} className="default-img"/>
                                            <div className="fsecd">
                                                <h6><b style={{fontSize:'20px',color:'#1890ff'}}>{item.patient.username}</b>( {item.patient.usersex+' '+(new Date().getFullYear()-item.patient.userbirthday.slice(0,4))+'岁'} )</h6>
                                                <p>接诊类型：{item.patient.registrationmes.receptiontype===0?'初诊':'复诊'}</p>
                                                <p>登记来源：线下门诊</p>
                                                <p>接诊医生：{item.doctor.doctor_name}</p>
                                            </div>
                                        </Card>
                                        <div className="hdsf">
                                            {item.patient.status===2||item.patient.status===3?<Button type="primary" style={{width:'100%'}} onClick={this.waitForTreat.bind(this,item)}>待理疗</Button>:
                                                <div>
                                                    <Button type="primary" style={{width:'33.33%'}} onClick={() => { browserHistory.push('assistantTherapyDetails/' + item.patient._id)}}>继续理疗</Button>
                                                    <Button type="primary" style={{width:'33.33%'}} onClick={() => { browserHistory.push('patientInfoDetails/' + item.patient._id) }}>完善资料</Button>
                                                    <Button type="primary" style={{width:'33.33%'}}>新增随访</Button>
                                                </div>
                                            }
                                        </div>
                                    </Col>}):'':''}
                            </Row>
                        </div>
                        {/*分页*/}
                        <div className="pagination">
                            <Pagination  showQuickJumper current={this.state.pageindex} total={ this.state.patientInfos.count||1} defaultPageSize={this.state.pagesize} onChange={this.changePage.bind(this)} />
                        </div>
                    </div>
                }
            </div>
        );
    }
}
export default AssistantTherapy;